package cn.tedu.order.management.service.impl;

import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.order.management.dao.persist.repository.RefundRepository;
import cn.tedu.order.management.pojo.entity.Order;
import cn.tedu.order.management.pojo.entity.OrderRefund;
import cn.tedu.order.management.pojo.param.OrderRefundParam;
import cn.tedu.order.management.service.RefundService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RefundServiceImpl implements RefundService {
    @Autowired
    private RefundRepository refundRepository;

    @Override
    public void addNew(Long orderId, OrderRefundParam orderRefundParam) {
        OrderRefund orderRefund=new OrderRefund();
        BeanUtils.copyProperties(orderRefundParam,orderRefund);
        orderRefund.setStatus(4);
        orderRefund.setOrderId(orderId);
        orderRefund.setRefundTime(LocalDateTime.now());
        Order order=new Order();
        order.setState(4);
        order.setId(orderId);
        refundRepository.updateById(4,orderId);
        int rows=refundRepository.addNew(orderRefund);
        if(rows!=1){
            String message = "退款失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
    }

    @Override
    public List<OrderRefund> list() {
        return refundRepository.getList();
    }

    @Override
    public void refundY(Long orderId) {
        refundRepository.updateById(3,orderId);
        refundRepository.updateByIdY(orderId);
    }

    @Override
    public void refundN(Long orderId) {
        refundRepository.updateById(0,orderId);
        refundRepository.updateByIdN(orderId);
    }

}
